#include "MainMenuState.h"

#define BUTTON_HEIGHT 75
#define BUTTON_WIDTH 270

MainMenuState::MainMenuState()
{
	mClickMenu=false;
	mWaitFrame=0;
}

MainMenuState::~MainMenuState()
{
	//--
}

void MainMenuState::OnEvent(SDL_Event *Event)
{
	GameState::OnEvent(Event);
}

bool MainMenuState::OnInit(SDL_Surface* p_SurfDisplay)
{
	//std::cout<<" INIT: MainMenuState  "<<std::endl;
	mMainMenuImage=GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\mainmenu.png",1);

	mButtonPlay1= new AnimatedObject();
	mButtonPlay1->setScreen(*p_SurfDisplay);
	mButtonPlay1->setPosition(Game::PosButtonMenuX,Game::PosButtonMenuY);
	mButtonPlay1->setSpeed(20);
	mButtonPlay1->setVisibleWhenNotPlaying(true);
	mButtonPlay1->setVisibleFrameWhenNotPlaying(0);

	SDL_Surface* temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\button_01.png",1);
	mButtonPlay1->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\button_02.png",1);
	mButtonPlay1->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\button_03.png",1);
	mButtonPlay1->AddFrame(temp);


	mButtonPlay2= new AnimatedObject();
	mButtonPlay2->setScreen(*p_SurfDisplay);
	mButtonPlay2->setPosition(Game::PosButtonMenuX,Game::PosButtonMenuY+Game::PosButtonMenuSpacingY);
	mButtonPlay2->setSpeed(20);
	mButtonPlay2->setVisibleWhenNotPlaying(true);
	mButtonPlay2->setVisibleFrameWhenNotPlaying(0);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\button2_01.png",1);
	mButtonPlay2->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\button2_02.png",1);
	mButtonPlay2->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\button2_03.png",1);
	mButtonPlay2->AddFrame(temp);

	mButtonExit= new AnimatedObject();
	mButtonExit->setScreen(*p_SurfDisplay);
	mButtonExit->setPosition(Game::PosButtonMenuX,Game::PosButtonMenuY+(2*Game::PosButtonMenuSpacingY));
	mButtonExit->setSpeed(20);
	mButtonExit->setVisibleWhenNotPlaying(true);
	mButtonExit->setVisibleFrameWhenNotPlaying(0);

	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\buttonExit_01.png",1);
	mButtonExit->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\buttonExit_02.png",1);
	mButtonExit->AddFrame(temp);
	temp = GraphicsUtil::getInstance().Create_SDLSurface("resources\\images\\buttonExit_03.png",1);
	mButtonExit->AddFrame(temp);

	Game::AudioManager::GetInstance().PlaySound(Game::Audio::SOUND_MENU);

	return true;
}

void MainMenuState::OnLoop()
{
	if(mClickMenu)
	{
		if((mWaitFrame++)>40)
		{
			Game::StateManager::GetInstance().SetState(Game::State::PLAYING);
			mClickMenu=false;
		}
	}
	//else
	//{
		//std::cout<<" LOOP: MainMenuState  "<<std::endl;
		if(mCursorXValue>Game::PosButtonMenuX && mCursorXValue<Game::PosButtonMenuX+BUTTON_WIDTH
		   && mCursorYValue>Game::PosButtonMenuY && mCursorYValue<Game::PosButtonMenuY+BUTTON_HEIGHT)
		{
			mButtonPlay1->setVisibleFrameWhenNotPlaying(1);
			if(mCursorClicked)
			{
				mButtonPlay1->setVisibleFrameWhenNotPlaying(2);
				mClickMenu=true;
				//Game::StateManager::GetInstance().SetState(Game::State::PLAYING);
			}
		}
		else
		{
			mButtonPlay1->setVisibleFrameWhenNotPlaying(0);
		}

		if(mCursorXValue>Game::PosButtonMenuX && mCursorXValue<Game::PosButtonMenuX+BUTTON_WIDTH
		   && mCursorYValue>Game::PosButtonMenuY+Game::PosButtonMenuSpacingY  && mCursorYValue<Game::PosButtonMenuY+Game::PosButtonMenuSpacingY + BUTTON_HEIGHT)
		{
			mButtonPlay2->setVisibleFrameWhenNotPlaying(1);
			if(mCursorClicked)
			{
				mButtonPlay2->setVisibleFrameWhenNotPlaying(2);
			}
		}
		else
		{
			mButtonPlay2->setVisibleFrameWhenNotPlaying(0);
		}


		if(mCursorXValue>Game::PosButtonMenuX && mCursorXValue<Game::PosButtonMenuX+BUTTON_WIDTH
		   && mCursorYValue>Game::PosButtonMenuY+(2*Game::PosButtonMenuSpacingY)  && mCursorYValue<Game::PosButtonMenuY+(2*Game::PosButtonMenuSpacingY) +BUTTON_HEIGHT)
		{
			mButtonExit->setVisibleFrameWhenNotPlaying(1);
			if(mCursorClicked)
			{
				Game::StateManager::GetInstance().SetState(Game::State::QUIT);
				mButtonExit->setVisibleFrameWhenNotPlaying(2);
			}
		}
		else
		{
			mButtonExit->setVisibleFrameWhenNotPlaying(0);
		}

	//}
}

void MainMenuState::OnRender(SDL_Surface *p_SurfDisplay)
{
	//std::cout<<" RENDERRR: MainMenuState  "<<std::endl;
	GraphicsUtil::getInstance().Render_SDLSurface(p_SurfDisplay,mMainMenuImage, 0,0);

	//mButtonPlay1->setScreen(*p_SurfDisplay);
	mButtonPlay1->Render();

	//mButtonPlay2->setScreen(*p_SurfDisplay);
	mButtonPlay2->Render();

	//mButtonExit->setScreen(*p_SurfDisplay);
	mButtonExit->Render();


}

void MainMenuState::OnCleanup()
{
	std::cout<<" CLEAN UPPP: MainMenuState  "<<std::endl;
	delete mButtonPlay1;
	delete mButtonPlay2;
	delete mButtonExit;
	GraphicsUtil::getInstance().ClearSDLSurfaces();
}